home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / pascal.txt < prev    next >
Text File  |  1994-09-21  |  41KB  |  1,040 lines

  1.  
  2.                  FUN
  3.   Imagine that you're running for President against Calvin 
  4. Coolidge. To win, you must pass Cal in the polls.
  5.   You must pass Cal. ``Pass Cal'' is the correct way to pronounce 
  6. ``PASCAL'', the name of the computer language that programmers 
  7. are falling in love with.
  8.   PASCAL is harder to learn than BASIC. But once you've learned 
  9. PASCAL, you have amazing power: you can write fancy programs more 
  10. easily in PASCAL than in BASIC because PASCAL helps organize your 
  11. thinking; and your programs will run faster, too!
  12.   American teachers require kids to master LOGO in elementary 
  13. school, BASIC in high school, and PASCAL in college. Gifted kids 
  14. are given the opportunity to start BASIC and PASCAL even sooner 
  15. ___ and so are kids in progressive schools.
  16.   Many high-school seniors take a PASCAL test given by the 
  17. College Board. Seniors who pass can get ``advanced placement'' 
  18. college credit and skip the college's first year of computer 
  19. courses.
  20.   The nicest kinds of PASCAL are Quick PASCAL (published by 
  21. Microsoft) and Turbo PASCAL (published by Borland). They're easy 
  22. to understand, run quickly, and cost little.
  23.   Quick PASCAL runs on the IBM PC and lists for just $99. 
  24. Discount dealers sell it for just $42.
  25.   Turbo PASCAL is available for the IBM PC, Mac, and computers 
  26. using the CP/M operating system. The newest version of Turbo 
  27. PASCAL for the IBM PC is Turbo PASCAL 7, which discount dealers 
  28. sell for $99.
  29.   If you're on a tight budget, which should you buy ___ Quick 
  30. PASCAL or Turbo PASCAL? Since they're very similar to each other, 
  31. you'll probably buy Quick PASCAL because it costs less. Another 
  32. advantage of Quick PASCAL is that it lets you edit your programs 
  33. more easily than Turbo PASCAL. On the other hand, Turbo PASCAL 
  34. runs your programs faster, consumes less RAM and disk space, 
  35. comes with instruction manuals that are larger and more thorough, 
  36. provides on-screen tutorials that are easier to use, understands 
  37. better commands for advanced programming, and is the standard 
  38. against which all other versions are judged. (Quick PASCAL was 
  39. invented just to imitate Turbo PASCAL more cheaply.)
  40.   This chapter explains how to use Quick PASCAL and the most 
  41. common versions of Turbo PASCAL (versions 4, 5.5, and 6).
  42.   If you have Turbo PASCAL 7, follow the instructions for Turbo 
  43. PASCAL 6, which is similar. If you have Turbo PASCAL version 5, 
  44. follow the instructions for version 5.5.
  45.   I'll comment on how other versions of PASCAL differ.
  46.                                                Copy to the hard disk
  47.                                          Quick PASCAL and Turbo 
  48. PASCAL come on a pile of floppy disks. You should copy those 
  49. disks to your hard disk. Here's how.
  50.                                          Quick PASCAL Turn on the 
  51. computer without any floppy in drive A.
  52.                                          The original version of 
  53. Quick PASCAL comes on five 5¼-inch floppy disks. When you see the 
  54. C prompt, put the Quick PASCAL Setup/Utilities Disk in drive A 
  55. and type ``a:''. The computer will display an A prompt. Type 
  56. ``setup''. The computer will say ``Microsoft Quick Pascal Setup 
  57. Program''.
  58.                                          Press ENTER twice. The 
  59. computer will say ``Easy Setup Menu''. Type the letter I. Press 
  60. ENTER four times.
  61.                                          Put the Quick PASCAL 
  62. Program Disk in drive A, and press ENTER. Put the Quick PASCAL 
  63. Advisor Disk in drive A, and press ENTER. Put the Quick PASCAL 
  64. Libraries Disk in drive A, and press ENTER. Put the Quick PASCAL 
  65. Express Disk in drive A; press ENTER twice.
  66.                                          Press R, so that the 
  67. computer says ``Setup Main Menu''. Press X, so that you exit to 
  68. DOS. You'll see an A prompt.
  69.                                          Turn off the computer, 
  70. so you can start fresh.
  71.                                          Turbo PASCAL 4 Turn on 
  72. the computer without any floppy in drive A.
  73.                                          After the C prompt, type 
  74. ``md turbo'' (so you're making a subdirectory called TURBO). 
  75. After the next C prompt, type ``cd turbo'' (so you're changing to 
  76. the TURBO subdirectory).
  77.                                          Turbo PASCAL 4 comes on 
  78. three floppy disks. Put one of those disks in drive A, and type 
  79. ``copy a:*.*'' (which copies all the floppy's files onto the hard 
  80. disk); follow the same procedure for the other two disks.
  81.                                          Turn off the computer, 
  82. so you can start fresh.
  83.                                          Turbo PASCAL 5.5 and 6 
  84. Turn on the computer without any floppy in drive A.
  85.                                          Your Turbo PASCAL comes 
  86. on four 5¼-inch floppy disks. When you see the C prompt, put the 
  87. Install/Compiler disk in drive A, and type ``a:install''. The 
  88. computer will say ``Turbo PASCAL Installation Utility''.
  89.                                          Press ENTER four times. 
  90. You'll see ``C:\TP''. Press the BACKSPACE key, then type URBO so 
  91. you see ``C:\TURBO''. At the end of the URBO, press ENTER.
  92.                                          Press the F9 key.
  93.                                          If you're using version 
  94. 6, put the Turbo Vision/Tour disk in drive A, press ENTER, put 
  95. the Help disk in drive A, press ENTER, put the BGI/Utilities disk 
  96. in drive A, and press ENTER twice. (If you're using version 5.5, 
  97. put the Tour/Online Help disk in drive A, press ENTER, put the 
  98. OOP/Demos/BGI/Doc disk in drive A, press ENTER, put the 
  99. Utilities/Misc disk in drive A, and press ENTER twice.)
  100.                                          You'll see an A prompt. 
  101. Turn off the computer, so you can start fresh.
  102.             Start PASCAL
  103.   To start using PASCAL, turn on the computer without any floppy 
  104. in drive A.
  105.   If you've put the DO.BAT file onto your hard disk (as I 
  106. recommended in the MS-DOS chapter), your life is easy! Just type 
  107. ``do qp'' to do Quick PASCAL; type ``do turbo'' to do Turbo 
  108. PASCAL.
  109.   If you have not put DO.BAT onto your hard disk, your life is 
  110. harder! You must type ``cd qp'' and then ``qp'' to do Quick 
  111. PASCAL; you must type ``cd turbo'' and then ``turbo'' to do Turbo 
  112. PASCAL.
  113.   Press the CAPS LOCK key, so the computer will automatically 
  114. capitalize everything you type (and your typing will look like 
  115. the examples in this chapter).
  116.   Here's what to do next.
  117.   Quick PASCAL You don't have to do anything!
  118.   Turbo PASCAL 4 Press the E key (which means ``Edit''). Make 
  119. sure the cursor is at a blank part of the screen. (If it's not, 
  120. press the DELETE key several times.)
  121.   Turbo PASCAL 5.5 Press the F6 key (so the cursor moves to the 
  122. bottom of the screen). Then press the F6 key while holding down 
  123. the Alt key (so the bottom of the screen says ``Output''). Then 
  124. tap the F6 key without the Alt key (so the cursor moves to the 
  125. top part of the screen).
  126.   Turbo PASCAL 6 Press F10 then W then the letter O (so the 
  127. cursor moves to the bottom of the screen). Press F10 again then W 
  128. then T. Press F6.
  129.  
  130.            How to program
  131.   For example, type this PASCAL program:
  132. BEGIN;
  133. WRITELN('I WOULD LIKE TO KISS');
  134. WRITELN('YOUR BOTTLE OF WINE');
  135. END.
  136.   The program begins with the word BEGIN and ends with the word 
  137. END. Every line ends with a semicolon, except that the bottom 
  138. line ends with a period. The middle lines say WRITELN, which 
  139. tells the computer to WRITE a LiNe.
  140.   Naming the program If you wish, you can put an extra line at 
  141. the top of the program, to give the program a name. The name can 
  142. be up to 8 letters long. For example, if you want to name the 
  143. program WINE, you can begin the program by saying PROGRAM WINE, 
  144. like this:
  145. PROGRAM WINE;
  146. BEGIN;
  147. WRITELN('I WOULD LIKE TO KISS');
  148. WRITELN('YOUR BOTTLE OF WINE');
  149. END.
  150.   Colors If your screen can display colors, here's what you see. 
  151. In Turbo PASCAL, the program is yellow. In Quick PASCAL, most of 
  152. the program is white, but the keywords (PROGRAM, BEGIN, and END) 
  153. turn purple and the strings (`I WOULD LIKE TO KISS' and `YOUR 
  154. BOTTLE OF WINE') turn light blue.
  155.                                          Run the program When you 
  156. finish typing the program, tell the computer to run it. Here's 
  157. how. For Turbo PASCAL 4, press F10 then R. For Turbo PASCAL 5.5 & 
  158. 6, press F10 then type RR. For Quick PASCAL, press F5; and if the 
  159. computer asks ``Rebuild?'', press ENTER.
  160.                                          If you typed the program 
  161. correctly, the computer will write:
  162. I WOULD LIKE TO KISS
  163. YOUR BOTTLE OF WINE
  164.                                          If you typed the program 
  165. incorrectly, the computer will say:
  166. Error
  167. (If you're using Quick PASCAL, then press ENTER.) The computer 
  168. will put the cursor near your error. Correct the error (by using 
  169. the arrow keys, DELETE key, and other word-processing keys), then 
  170. tell the computer to run the program again.
  171.                                          After a good run After 
  172. the computer has run the program successfully, do the following: 
  173. for Quick PASCAL, press ENTER; for Turbo PASCAL 4, press ENTER 
  174. then E; for Turbo PASCAL 5.5 and 6, you don't have to do 
  175. anything!
  176.                                          You'll see the program 
  177. again. If you wish, edit the program again and run the edited 
  178. version.
  179.                                           Other versions Some 
  180. versions of PASCAL require you to say PROGRAM WINE at the top of 
  181. the program; if you don't say PROGRAM WINE, those versions gripe.
  182.                                          Ancient versions require 
  183. the top line to say this:
  184. PROGRAM WINE(INPUT,OUTPUT);
  185.                                          Some Apple 2 and Mac 
  186. versions automatically indent all lines between BEGIN and END and 
  187. force you to omit the semicolon next to the word BEGIN.
  188.  
  189.                                                  Advanced editing
  190.                                          While typing and editing 
  191. your program, you can edit faster by using these tricks. . . . 
  192.                                          To delete a whole line, 
  193. move to that line. Then tap the Y key (which means ``Yank it 
  194. out'') while holding down the CONTROL key.
  195.                                          To insert an extra line 
  196. in the middle of your program, move to where you want the extra 
  197. line to begin. Press the ENTER key, then the up-arrow.
  198.                                          Here's how to delete or 
  199. move a block of text. . . . 
  200.                                          Turbo PASCAL 6 and Quick 
  201. PASCAL Put the cursor at the block's beginning. Hold down the 
  202. SHIFT key while moving to the block's end, so the entire block 
  203. changes color.
  204.                                          Then say what you want 
  205. to do to the block. If you want to delete the block, press the 
  206. DELETE key for Quick PASCAL; press CONTROL with DELETE for Turbo 
  207. PASCAL 6. If you want to move the block instead, press SHIFT with 
  208. DELETE, then point where you want the block to appear and press 
  209. SHIFT with INSERT.
  210.                                          Turbo PASCAL 4 and 5.5 
  211. Put the cursor at the block's beginning. Press CONTROL with K 
  212. (which means ``kommand''), then press B (which means ``block'').
  213.                                          Point at the character 
  214. after the block's end. Press CONTROL with K, then press K (which 
  215. means ``klose''). The entire block changes color.
  216.                                          Then say what you want 
  217. to do to the block. If you want to delete the block, press 
  218. CONTROL K then Y (``which means ``yank''). If you want to move 
  219. the block instead, point where you want the block to appear and 
  220. press CONTROL K then V (which means ``visible'').
  221.           Save your program
  222.   Here's how to copy your program onto the hard disk.
  223.   Quick PASCAL Press the Alt key. Say ``File Save'' by typing FS. 
  224. If the computer says ``File Name'', type a name for the program 
  225. (such as WINE) and press ENTER.
  226.   Turbo PASCAL 4 and 5.5 Press the F2 key. If the computer says 
  227. ``Rename'', type a name for the program (such as WINE) and press 
  228. ENTER.
  229.   Turbo PASCAL 6 Press the F2 key. If the computer says ``Save 
  230. file as'', type a name for the program (such as WINE) and press 
  231. ENTER.
  232.  
  233.            Switch programs
  234.   Here's how to switch to other programs.
  235.   Quick PASCAL Press CONTROL with F4. If the computer asks ``Do 
  236. you want to save?'', press N. The screen becomes blank. Press the 
  237. Alt key.
  238.   If you want to invent a new program, say ``File New'' (by 
  239. typing FN). If instead you want to retrieve a saved program, do 
  240. this: say ``File Open'' (by typing FO), type the program's name 
  241. (such as WINE), and press ENTER.
  242.   Turbo PASCAL 4 and 5.5 If you want to invent a new program, 
  243. press F10 then type FN (which means File New). If instead you 
  244. want to retrieve a saved program, do this: press F3 and ENTER; 
  245. point at the program you want to use (by using the arrow keys and 
  246. PAGE DOWN key) and press ENTER.
  247.   If the computer asks ``Save?'', press N.
  248.   Turbo PASCAL 6 Press Alt with F3. If the computer asks 
  249. ``Save?'', press N.
  250.   If you want to invent a new program, press F10 then type FN 
  251. (which means File New). If instead you want to retrieve a saved 
  252. program, do this: press F3 and ENTER; point at the program you 
  253. want to use (by using the arrow keys) and press ENTER.
  254.  
  255.           Multiple writing
  256.   This program belongs in your bathroom:
  257. BEGIN;
  258. WRITELN('FAR','TIN','G');
  259. END.
  260. The computer will write FAR, TIN, and G all on the same line, 
  261. like this:
  262. FARTING
  263.  
  264.                 Exit
  265.   When you finish using PASCAL, here's how to return to the DOS 
  266. prompt.
  267.   Quick PASCAL Press the Alt key. Say ``File eXit'' (by typing 
  268. FX). If the computer asks ``Do you want to save?'', press N.
  269.   If you're using a fancy color monitor (such as VGA), you'll 
  270. discover that Quick PASCAL has changed the monitor's colors. To 
  271. reset the colors, turn the computer off and then back on.
  272.   Turbo PASCAL Press Alt with X. If the computer asks ``Save?'', 
  273. press N.
  274.                                                      EXE files
  275.                                          The computer can't 
  276. really understand PASCAL. Whenever you tell the computer to run a 
  277. PASCAL program, the computer secretly creates a second version of 
  278. your program. The second version is written in machine language 
  279. instead of PASCAL. (Machine language is the only language the 
  280. computer really understands.) Then the computer runs the 
  281. machine-language version of your program.
  282.                                          The program you typed 
  283. (in PASCAL) is called the source code. The program the computer 
  284. runs (in machine language) is called the object code.
  285.                                          When the computer is 
  286. translating your program from PASCAL to machine language (from 
  287. source code to object code), the computer is said to be compiling 
  288. your program. Since Quick PASCAL and Turbo PASCAL make the 
  289. computer compile your PASCAL programs, Quick PASCAL and Turbo 
  290. PASCAL are called PASCAL compilers.
  291.                                          Example Suppose you type 
  292. a PASCAL program, then tell the computer to save it on the hard 
  293. disk and call it WINE. The computer will copy it to your hard 
  294. disk's PASCAL subdirectory, where it will be called ``WINE.PAS''. 
  295. The ``.PAS'' means ``source code written in PASCAL''.
  296.                                          When you tell the 
  297. computer to run the WINE.PAS program, the computer translates it 
  298. to machine language. The machine-language version (the object 
  299. code) is called ``WINE.EXE''. The ``.EXE'' means ``executable 
  300. object code written in machine language''.
  301.                                          If you're using Quick 
  302. PASCAL, the computer automatically puts WINE.EXE in your disk's 
  303. PASCAL subdirectory. If you're using Turbo PASCAL instead, the 
  304. computer puts WINE.EXE in the RAM but not on disk ___ unless you 
  305. give a special ``Destination Disk'' command.
  306.                                          After the computer has 
  307. run your program and put WINE.EXE on disk, you can run the 
  308. WINE.EXE machine-language program without using PASCAL. Here's 
  309. how: quit PASCAL, then do a ``cd'' to the PASCAL subdirectory, 
  310. then type ``wine''.
  311.                                          Experiment Try the 
  312. following experiment. . . . 
  313.                                          Step 1: type a PASCAL 
  314. program.
  315.                                          Step 2: tell the 
  316. computer to save the program and call it WINE. The computer will 
  317. call your program WINE.PAS and copy it to PASCAL's subdirectory. 
  318. (If you're using Turbo PASCAL, the computer will rename a 
  319. previous WINE.PAS to WINE.BAK.)
  320.                                          Step 2½: if you're using 
  321. Turbo PASCAL, say ``Destination Disk''. Here's how. Press F10 
  322. then C. If you see ``Destination Memory'' instead of 
  323. ``Destination Disk'', press D. Then return to the editor (by 
  324. pressing F6 for version 4, Esc for version 5.5).
  325.                                          Step 3: run the program. 
  326. The computer will invent WINE.EXE (a machine-language version of 
  327. WINE) and put it in PASCAL's subdirectory. Then the computer will 
  328. run that machine-language program. (If you're using Quick PASCAL, 
  329. the computer will also produce a WINE.QDB to help the Quick 
  330. DeBugger.)
  331.                                          Step 4: exit from 
  332. PASCAL, so you see the DOS prompt.
  333.                                          Step 5: notice that you 
  334. can run WINE.EXE without using PASCAL. Specifically, go into 
  335. PASCAL's subdirectory (by typing ``cd \qp'' for Quick PASCAL, or 
  336. ``cd \turbo'' for Turbo PASCAL), then type ``wine'' (which runs 
  337. the WINE.EXE program).
  338.  
  339.                 MATH
  340.   PASCAL distinguishes between integers and real numbers. Here's 
  341. how PASCAL defines them. . . . 
  342.  
  343.               Integers
  344.   An integer contains no decimal point and no exponent.
  345. IntegerNot an integerComment
  346. -27   -27.0     An integer contains no decimal point.
  347. 50000 5E4       An integer contains no exponent.
  348.  
  349.             Real numbers
  350.   A real number contains a decimal point or the letter E. For 
  351. example, it can be 0.37 or 5.0 or 6E24. If it contains a decimal 
  352. point, you should put digits before and after the decimal point.
  353. CorrectIncorrect
  354. 0.37  .37
  355. 5.0   5.
  356.    Other versions Old versions of PASCAL require you to put a 
  357. decimal point in every real number; so instead of saying 6E24, 
  358. you must say 6.0E24.
  359.  
  360.              Arithmetic
  361.   Like BASIC, PASCAL lets you do arithmetic by using the symbols 
  362. +, -, *, and /.
  363.   If you combine real numbers, the answer is real:
  364. 4.9+2.1 is the real number 7.0 (not 7)
  365. 5.0-5.0 is the real number 0.0 (not 0)
  366.   If you combine a real with an integer, the answer is still 
  367. real:
  368. 3.5*2 is the real number 7.0 (not 7)
  369. 3/.5  is the real number 6.0 (not 6)
  370.   If you combine integers, the answer is an integer, except for 
  371. division:
  372. 7+4 is the integer 11
  373. 7-4 is the integer 3
  374. 7*4 is the integer 28
  375. 7/4 is the real number 1.75
  376. This program makes the computer do all that arithmetic:
  377. BEGIN;
  378. WRITELN(7+4);
  379. WRITELN(7-4);
  380. WRITELN(7*4);
  381. WRITELN(7/4);
  382. END.
  383. The computer will write:
  384. 11
  385. 3
  386. 28
  387.  1.7500000000E+00
  388. (Quick PASCAL writes extra zeros, like this: 
  389. 1.75000000000000E+0000.)
  390.                                          Let's try writing all 
  391. those numbers on the same line:
  392. BEGIN;
  393. WRITELN(7+4,7-4,7*4,7/4);
  394. END.
  395. The computer will write:
  396. 11328 1.7500000000E+00
  397.                                          Like BASIC, PASCAL lets 
  398. you use parentheses to indicate order of operations.
  399.                                           Other versions Some 
  400. versions of PASCAL automatically write blank spaces in front of 
  401. integers.
  402.                                          Some versions of PASCAL 
  403. write 1.75 instead of 1.7500000000E+00. Some Apple 2 and Mac 
  404. versions automatically round the answer and write 1.8e+0.
  405.  
  406.                                                      Functions
  407.                                          PASCAL lets you use 
  408. these functions:
  409. Function                                       Meaning
  410. ABS(-6)                                        the absolute value 
  411. of -6; it's 6
  412. SQR(3)                                         the square of 3; 
  413. it's 9
  414. SQRT(9)                                        the square root of 
  415. 9; it's 3.0
  416. SIN(2)                                         the sine of 2 
  417. radians
  418. COS(2)                                         the cosine of 2 
  419. radians
  420. ARCTAN(2)                                      the arctangent of 
  421. 2, in radians
  422. EXP(5)                                         e5, where e is 
  423. 2.71828182845904523536
  424. LN(9)                                          the natural 
  425. logarithm of 9; it's loge 9
  426. The number in parentheses can be either an integer or a real. The 
  427. computer's answer is usually a real (exception: for ABS and SQR, 
  428. the computer's answer is an integer if the number in parentheses 
  429. is an integer).
  430.                                          For example, this 
  431. program computes the square root of 9:
  432. BEGIN;
  433. WRITELN(SQRT(9));
  434. END.
  435. The computer will write:
  436.  3.0000000000E+00
  437.                                          Don't confuse SQR with 
  438. SQRT. The SQR means square; the SQRT means square root. To find 
  439. the square of 3, say SQR(3) or 3*3. In BASIC, you could say 3^2; 
  440. but in PASCAL, you can't use the symbol ^; use SQR instead.
  441.                                          To turn a real number 
  442. into an integer, use these functions:
  443. Function                                       Meaning
  444. ROUND(3.9)                                     3.9 rounded to the 
  445. nearest integer;                                                 
  446. it's 4
  447. TRUNC(3.9)                                     3.9 truncated (by 
  448. deleting the .9);                                                
  449. it's 3
  450.                                           Other versions For 
  451. UCSD PASCAL, say ATAN instead of ARCTAN.
  452.                                          If you're using UCSD 
  453. PASCAL on an Apple and want to use the functions SQRT, SIN, COS, 
  454. ATAN, EXP, and LN, you must insert this line immediately under 
  455. the line that says PROGRAM:
  456. USES TRANSCEND;
  457.  
  458.  
  459.           SIMPLE VARIABLES
  460.   You can use variables:
  461. Program             Meaning
  462. VAR                 Here are the variables. . . . 
  463.    FRED,MARTHA: INTEGER;  FRED & MARTHA integers;
  464.    JILL,TOM: REAL;    JILL & TOM are reals.
  465. BEGIN;              Now let's begin the program.
  466. FRED:=2;            FRED (an integer) is 2.
  467. MARTHA:=4+5;        MARTHA (an integer) is 9.
  468. JILL:=9.2;          JILL (a real) is 9.2.
  469. TOM:=1.4+2.3;       TOM (a real) is 3.7.
  470. WRITELN(FRED*MARTHA,JILL+TOM);Write 18 and 1.2900000000E+01.
  471.   The top line says VAR. The word VAR doesn't have a semicolon 
  472. after it; the lines underneath VAR are indented. The indentation 
  473. is optional but a good habit. To indent easily, tap the TAB key.
  474.   For Quick PASCAL, the TAB key indents 8 spaces. For Turbo 
  475. PASCAL, the TAB key indents just enough to get past the word 
  476. above (VAR).
  477.   On the indented lines, say which variables are integers and 
  478. which are reals.
  479.   Once you've indented a line, the computer automatically indents 
  480. all the lines underneath it. If the computer automatically 
  481. indents a line that you don't want to indent, here's how to undo 
  482. the indentation: for Turbo PASCAL 5.5, press the BACKSPACE key 
  483. once; for Turbo PASCAL 4, press the left-arrow key several times; 
  484. for Quick PASCAL, tap the TAB key while holding down the SHIFT 
  485. key.
  486.   In PASCAL, a variable's name can be as long as you like: the 
  487. name can be FRED or WASHINGTON or even 
  488. SUPERCALIFRAGILISTICEXPIALIDOCIOUS. The name can consists of 
  489. letters and digits, but must begin with a letter.
  490.   In PASCAL, you must put a colon in front of the equal sign. . . 
  491. CorrectIncorrect
  492. FRED:=2;FRED=2;
  493.   If you want to add a line saying PROGRAM WINE, put that line 
  494. above the VAR line.
  495.   You can't set an integer variable equal to a real. For example, 
  496. if you say ___ 
  497. VAR
  498.    ANN: REAL;
  499.    BILL: INTEGER;
  500. BEGIN;
  501. ANN:=3.9;
  502. you cannot then say:
  503. BILL:=ANN;
  504. Instead, you must say ___ 
  505. BILL:=ROUND(ANN);
  506. or
  507. BILL:=TRUNC(ANN);
  508.    Other versions Some versions of PASCAL do not automatically 
  509. indent the lines for you.
  510.   Old versions of PASCAL look at just the first 8 characters of a 
  511. variable's name, and ignore the rest of the name.
  512.                                                       READLN
  513.                                          Instead of saying INPUT, 
  514. PASCAL says READLN.
  515. VAR
  516.    X: REAL;
  517. BEGIN;
  518. WRITELN('WHAT IS YOUR FAVORITE NUMBER?');
  519. READLN(X);
  520. WRITELN('ITS SQUARE ROOT IS',SQRT(X);
  521. END.
  522. When you run the program, the computer asks:
  523. WHAT IS YOUR FAVORITE NUMBER?
  524. Then the READLN(X) statement makes the computer wait for you to 
  525. input a value of X. The computer expects you to input a real 
  526. number; if you input an integer instead, the computer will 
  527. automatically turn it into a real. For example, if you input 9, 
  528. the computer will automatically turn it into 9.0 and will write:
  529. ITS SQUARE ROOT IS 3.0000000000E+00
  530.                                           Other versions For old 
  531. versions of PASCAL, say READ instead of READLN.
  532.  
  533.  
  534.                  IF
  535.   The computer can criticize your age:
  536. VAR
  537.    AGE: INTEGER;
  538. BEGIN;
  539. WRITELN('HOW OLD ARE YOU?');
  540. READLN(AGE);
  541. IF AGE<18 THEN
  542.    BEGIN;
  543.    WRITELN('YOU ARE STILL A MINOR.');
  544.    WRITELN('AH, THE JOYS OF YOUTH!');
  545.    END;
  546. WRITELN('GLAD TO MEET YOU.');
  547. END.
  548.   That program makes the computer ask:
  549. HOW OLD ARE YOU?
  550. If you input a number less than 18, the computer replies:
  551. YOU ARE STILL A MINOR.
  552. AH, THE JOYS OF YOUTH!
  553. GLAD TO MEET YOU.
  554. If you input a number that's at least 18, the computer says just 
  555. this:
  556. GLAD TO MEET YOU.
  557.   In that program, the line that says ``IF'' is a heading (the 
  558. lines underneath it are indented), so do not put a semicolon at 
  559. the end of that line!
  560.  
  561.                 ELSE
  562.   To make your program fancier, insert the shaded lines:
  563. VAR
  564.    AGE: INTEGER;
  565. BEGIN;
  566. WRITELN('HOW OLD ARE YOU');
  567. READ(AGE);
  568. IF AGE<18 THEN
  569.    BEGIN;
  570.    WRITELN('YOU ARE STILL A MINOR.');
  571.    WRITELN('AH, THE JOYS OF YOUTH!');
  572.    END 
  573. ELSE                                               
  574.     BEGIN;                                         
  575.     WRITELN('I AM GLAD TO HEAR YOU ARE AN ADULT!');
  576.     WRITELN('NOW WE CAN HAVE SOME ADULT FUN!');    
  577.     END;                                           
  578. WRITELN('GLAD TO MEET YOU.');
  579. END.
  580.   If your age is less than 18, the computer will say:
  581. YOU ARE STILL A MINOR.
  582. AH, THE JOYS OF YOUTH!
  583. GLAD TO MEET YOU.
  584. If your age is not less than 18, the computer will say:
  585. GLAD TO HEAR YOU ARE AN ADULT!
  586. NOW WE CAN HAVE SOME ADULT FUN!
  587. GLAD TO MEET YOU.
  588.   In that program, the line that says ``ELSE'' is a heading, so 
  589. do not put a semicolon after the ``ELSE''. Immediately above the 
  590. word ELSE, you'll see the word END; do not put a semicolon after 
  591. that END.
  592.                                                       Symbols
  593.                                          Like BASIC, PASCAL uses 
  594. these symbols in the IF line:
  595. <      >      =      <=      >=      <>      AND      OR
  596.                                          In the IF line, the 
  597. symbol for ``equals'' is ``=''; outside the IF line, the symbol 
  598. for ``equals'' is ``:=''.
  599.                                          Instead of saying ___ 
  600. IF I=3 OR I=8 OR I=25 OR I=95 THEN
  601. you can say:
  602. IF I IN [3,8,25,95] THEN
  603. That means ``If I is in this set of numbers ___ 3,8,25,95 ___ 
  604. then . . . ''
  605.  
  606.                 LOOPS
  607.   To create a loop, say FOR or REPEAT or WHILE. Here's how to use 
  608. those words.
  609.  
  610.                  FOR
  611.   Like BASIC, PASCAL uses the word FOR. This program makes the 
  612. computer get drunk:
  613. VAR
  614.    I: INTEGER;
  615. BEGIN;
  616. FOR I := 7 TO 10 DO
  617.     BEGIN;
  618.     WRITELN('I DRANK ',I,' BOTTLES OF BEER');
  619.     WRITELN('HOORAY!');
  620.     END;
  621. WRITELN('NOW I AM DEAD DRUNK');
  622. END.
  623.   Since the FOR line is a heading, do not put a semicolon at the 
  624. end of it. When you run the program, the computer will write:
  625. I DRANK 7 BOTTLES OF BEER
  626. HOORAY!
  627. I DRANK 8 BOTTLES OF BEER
  628. HOORAY!
  629. I DRANK 9 BOTTLES OF BEER
  630. HOORAY!
  631. I DRANK 10 BOTTLES OF BEER
  632. HOORAY!
  633. NOW I AM DEAD DRUNK
  634.   If you want the computer to count backwards ___ from 10 down to 
  635. 7 ___ change the FOR line to this:
  636. FOR I := 10 DOWNTO 7 DO
  637.  
  638.                REPEAT
  639.   This program plays a guessing game:
  640. VAR
  641.    GUESS: INTEGER;
  642. BEGIN;
  643. WRITELN('I AM THINKING OF A NUMBER FROM 1 TO 10');
  644. REPEAT
  645.       WRITELN('YOU HAVE NOT GUESSED MY NUMBER YET');
  646.       WRITELN('WHAT IS MY NUMBER?');
  647.       READLN(GUESS);
  648. UNTIL GUESS=6;
  649. WRITELN('CONGRATULATIONS! YOU GUESSED IT! MY NUMBER IS 6');
  650. END.
  651.   The computer begins the game by saying:
  652. I AM THINKING OF A NUMBER FROM 1 TO 10
  653. Then the computer REPEATs the following procedure several times: 
  654. it says YOU HAVE NOT GUESSED MY NUMBER YET, asks WHAT IS MY 
  655. NUMBER, waits for the human to input a guess, and checks whether 
  656. the human guessed 6. It repeats that procedure again and again, 
  657. UNTIL the human finally guesses 6. Then the computer says:
  658. CONGRATULATIONS! YOU GUESSED IT! MY NUMBER IS 6
  659.   In that program, the computer REPEATs the indented lines, UNTIL 
  660. GUESS is 6.
  661.   If a program contains the word REPEAT, it must also contain the 
  662. word UNTIL. The lines between REPEAT and UNTIL are done 
  663. repeatedly. After each time, the computer checks whether to 
  664. repeat again.
  665.   Since the REPEAT line's a heading, it has no semicolon.
  666.   Like an IF line, the UNTIL line can contain these symbols:
  667. <     >     =     <=     >=     <>     AND     OR     IN
  668.   Here's another example. Let's make the computer start
  669. with the number 3 and keep doubling it, like this:
  670. 3
  671. 6
  672. 12
  673. 24
  674. etc.
  675. Let's make the computer keep doubling but not go over 1000. So 
  676. altogether, let's make the computer write:
  677. 3
  678. 6
  679. 12
  680. 24
  681. 48
  682. 96
  683. 192
  684. 384
  685. 768
  686. THOSE ARE ALL THE NUMBERS BELOW 1000
  687.                                          Here's the program:
  688. VAR
  689.    I: INTEGER;
  690. BEGIN;
  691. I:=3;                                              Start I at 3.
  692. REPEAT                                             Repeat the 
  693. indented lines until I>=1000.
  694.       WRITELN(I);
  695.       I:=I*2;
  696. UNTIL I>=1000;
  697. WRITELN('THOSE ARE ALL THE NUMBERS BELOW 1000');
  698. END.
  699.  
  700.                                                        WHILE
  701.                                          The word WHILE resembles 
  702. the word REPEAT. For example, the previous program (which doubled 
  703. 3 repeatedly) can be rewritten to use WHILE:
  704. VAR
  705.    I: INTEGER;
  706. BEGIN;
  707. I:=3;                                              Start I at 3.
  708. WHILE I<1000 DO                                    If I<1000, do 
  709. the indented lines, and repeat them
  710.       BEGIN;                                       again and 
  711. again, as long as I remains below 1000.
  712.       WRITELN(I);
  713.       I:=I*2;
  714.       END;
  715. WRITELN('THOSE ARE ALL THE NUMBERS BELOW 1000');
  716. END.
  717.                                          The indented lines say 
  718. to WRITELN(I) and then double I. The computer repeats those 
  719. indented lines many times. Before each repetition, the computer 
  720. checks to make sure I is still below 1000. When I passes 1000, 
  721. the computer stops looping and proceeds to the next line, which 
  722. writes THOSE ARE ALL THE NUMBERS BELOW 1000.
  723.                                          These two structures 
  724. resemble each other:
  725. Using REPEAT                                       Using WHILE
  726. REPEAT                                             WHILE I<1000 
  727. DO
  728.       etc.                                               BEGIN;
  729. UNTIL I>=1000;                                           etc.
  730.                                                          END;
  731. Each of those structures makes the computer repeat the indented 
  732. lines and check whether I is still below 1000. If you say REPEAT, 
  733. the check is done at the loop's bottom, so the computer goes 
  734. through the loop once before checking. If you say WHILE, the 
  735. check is done at the loop's top, so the computer checks whether 
  736. I<1000 before doing the loop the first time.
  737.  
  738.                   LOGIC TRICKS
  739.   Every statement must end with a semicolon. You can put several 
  740. statements on the same line:
  741. Normal    Alternative
  742. A:=3;     A:=3; B:=7.6; WRITELN(A+B);
  743. B:=7.6;
  744. WRITELN(A+B);
  745.   You can continue a statement on the next line:
  746. Normal    Alternative
  747. A:=5-2+1; A:=
  748.           5-2
  749.           +1;
  750. But do not divide a statement in the middle of a word, number, 
  751. symbol, or string:
  752.                             Wrong, because  Wrong, because    
  753. Wrong, because
  754. Normal           Okay       middle of word  middle of number  
  755. middle of symbol
  756. IF AGE>=18 THEN  IF AGE     IF AG           IF AGE>=1         IF 
  757. AG>
  758.                  >=18 THEN  E>=18 THEN      8 THEN            =18 
  759. THEN
  760.  
  761.                Omitting semicolons
  762.   You can omit the semicolon after BEGIN:
  763. Normal    Alternative
  764. BEGIN;    BEGIN
  765.   You can omit a semicolon, if the next word is END:
  766. Normal    Alternative
  767. A:=3;     A:=3
  768. END;      END;
  769.   You can omit a semicolon, if the next word is UNTIL:
  770. Normal    Alternative
  771.       I=*2;      I=I*2
  772. UNTIL I>=1000;UNTIL I>=1000;
  773.  
  774.              Omitting BEGIN and END
  775.   If a heading heads just one indented line (besides BEGIN and 
  776. END), you can put that indented line next to the heading (and 
  777. omit the BEGIN and END).
  778. Long way    Short cut
  779. FOR I := 1 TO 9 DOFOR I := 1 TO 9 DO WRITELN(I);
  780.     BEGIN;
  781.     WRITELN(I);
  782.     END;
  783.  
  784. WHILE A<100 DOWHILE A<100 DO A:=A+7;
  785.       BEGIN;
  786.       A=A+7;
  787.       END;
  788.  
  789. IF AGE>=65 THENIF AGE>=65 THEN WRITELN('OLD');
  790.    BEGIN;
  791.    WRITELN('OLD');
  792.    END;
  793.  
  794. IF WEIGHT>220 THENIF WEIGHT>220 THEN WRITELN('FAT') ELSE 
  795. WRITELN('OKAY');
  796.    BEGIN;
  797.    WRITELN('FAT');
  798.    END
  799. ELSE
  800.    BEGIN;
  801.    WRITELN('OKAY');
  802.    END;
  803.  
  804. REPEAT      REPEAT A:=A+7 UNTIL A>100;
  805.       A:=A+7;
  806. UNTIL A>100;
  807.  
  808.  
  809.               Comments
  810.   To put comments into your program, surround the comment by 
  811. braces:
  812. BASIC         PASCAL
  813. 10 'I HATE COMPUTERS{I HATE COMPUTERS}
  814.   Do not put a semicolon after the comment.
  815.   If your screen can display colors, Quick PASCAL makes the 
  816. comments and braces turn green.
  817.    Other versions If your keyboard is old and lacks braces, use 
  818. parentheses and asterisks instead:
  819. (*I HATE COMPUTERS*)
  820.  
  821.                 GOTO
  822.   You can say GOTO.
  823. LABEL 10;
  824. BEGIN;
  825. WRITELN('MY DOG');
  826. GOTO 10;
  827. WRITELN('NEVER');
  828. 10: WRITELN('DRINKS WHISKEY');
  829. END.
  830.   The top line warns the computer that a future line will be 
  831. labeled ``line 10''. The rest of the program makes the computer 
  832. write MY DOG, then skip to line 10, which makes the computer 
  833. write DRINKS WHISKEY. Altogether, the computer will write:
  834. MY DOG
  835. DRINKS WHISKEY
  836.   Do not put a space between GO and TO.
  837.   Here's another example:
  838. LABEL 10;
  839. BEGIN;
  840. 10: WRITELN('LOVE');
  841. WRITELN('HATE');
  842. GOTO 10;
  843. END.
  844. The computer will write LOVE and HATE repeatedly:
  845. LOVE
  846. HATE
  847. LOVE
  848. HATE
  849. LOVE
  850. HATE
  851. etc.
  852. To abort the program, tap the C key while holding down the 
  853. CONTROL key. (For Quick PASCAL, then press ENTER.)
  854.   If your program contains lines numbered 10, 20, and 100, put 
  855. this statement at the top of your program:
  856. LABEL 10,20,100;
  857. Put that statement at the very top of your program: the only 
  858. statement that should go above it is the one saying PROGRAM. The 
  859. PROGRAM and LABEL lines go above all other lines ___ even above 
  860. VAR and BEGIN.
  861.   Each line number must be small: no higher than 9999.
  862.   In PASCAL, you'll rarely need to say GOTO. Instead, try using 
  863. the words IF, ELSE, FOR, REPEAT, and WHILE.
  864.    Other versions In UCSD PASCAL, if you want to say GOTO, you 
  865. must say (*$G+*) at the top of your program:
  866. (*$G+*)
  867. PROGRAM SKIPPER;
  868. LABEL 10;
  869. etc.
  870.  
  871.                                                     Procedures
  872.                                          This program teases you, 
  873. with the help of some insults:
  874. PROGRAM INSULT;
  875. BEGIN;
  876. WRITELN('YOU ARE STUPID');
  877. WRITELN('YOU ARE UGLY');
  878. END;
  879.  
  880. {MAIN ROUTINE}
  881. BEGIN;
  882. WRITELN('WE ALL KNOW...');
  883. INSULT;
  884. WRITELN('...AND YET WE LOVE YOU');
  885. END.
  886.  
  887.                                          That program begins by 
  888. defining INSULT to be this procedure:
  889. WRITELN('YOU ARE STUPID');
  890. WRITELN('YOU ARE UGLY');
  891. The main routine makes the computer write `WE ALL KNOW...', then 
  892. do the INSULT procedure, then write `...AND YET WE LOVE YOU'; 
  893. altogether, the computer will write:
  894. WE ALL KNOW...
  895. YOU ARE STUPID
  896. YOU ARE UGLY
  897. ...AND YET WE LOVE YOU
  898.                                          The PROCEDURE, called 
  899. INSULT, is a subroutine. In PASCAL, subroutines come before the 
  900. main routine. (In BASIC, subroutines come after the main routine 
  901. instead.)
  902.                                          Put a program's VAR 
  903. above any procedures:
  904. VAR                                                The program 
  905. uses a variable,
  906.    I: INTEGER;                                     called I, 
  907. which is an integer.
  908.  
  909. PROCEDURE DOUBLE;                                  The subroutine 
  910. is called DOUBLE.
  911. BEGIN;
  912. I:=2*I;                                            The subroutine 
  913. doubles the value of I
  914. WRITELN(I);                                        and writes the 
  915. new value of I.
  916. END;
  917.  
  918. {MAIN ROUTINE}                                     Here's the 
  919. main routine. . . .
  920. BEGIN;
  921. I:=7;                                              I starts at 7.
  922. DOUBLE;                                            Do subroutine 
  923. DOUBLE, which writes 14.
  924. DOUBLE;                                            Do DOUBLE 
  925. again, which writes 28.
  926. WRITELN(I+1);                                      Write 29.
  927. END.
  928.  
  929.  
  930.          ADVANCED VARIABLES
  931.   You've seen that a variable can stand for a number. This 
  932. section explains how to make a variable stand for a character, 
  933. array, or string.
  934.  
  935.                 CHAR
  936.   A variable can stand for a character:
  937. VAR
  938.    ANN,JOAN: CHAR;ANN and JOAN will each be a character.
  939. BEGIN;
  940. ANN:='U';   ANN is the character `U'.
  941. JOAN:='P';  JOAN is the character `P'.
  942. WRITELN(ANN,JOAN);The computer will write `U' and `P'.
  943. END.
  944. The computer will write U and P like this:
  945. UP
  946.   You can put a character variable after the word FOR:
  947. VAR
  948.    I: CHAR;
  949. BEGIN;
  950. FOR I := 'A' TO 'E' DO WRITELN(I);
  951. END.
  952. The computer will write:
  953. A
  954. B
  955. C
  956. D
  957. E
  958.  
  959.                 ARRAY
  960.   Let's make X be this list of real numbers: 4.2, 71.6, 8.3, 
  961. 92.6, 403.7, 1.4. Here's how:
  962. VAR
  963.    X: ARRAY [1..6] OF REAL;
  964. BEGIN;
  965. X[1]:=4.2;
  966. X[2]:=71.6;
  967. X[3]:=8.3;
  968. X[4]:=92.6;
  969. X[5]:=403.7;
  970. X[6]:=1.4;
  971. WRITELN(X[1]+X[2]+X[3]+X[4]+X[5]+X[6]);
  972. END.
  973. The computer will write the sum, 581.8.
  974.   Subscripts can be negative:
  975. VAR
  976.    Y: ARRAY [-2..3] OF REAL;
  977. BEGIN;
  978. Y[-2]:=400.1;
  979. Y[-1]:=274.1;
  980. Y[0]:=9.2;
  981. Y[1]:=8.04;
  982. Y[2]:=0.6;
  983. Y[3]:=-5.0;
  984. WRITELN(Y[-2]+Y[-1]+Y[0]+Y[1]+Y[2]+Y[3]);
  985. END.
  986. The computer will write the sum, 687.04.
  987.                                          Here's how to make Z be 
  988. a table having 6 rows and 4 columns of reals:
  989. VAR
  990.    Z: ARRAY [1..6, 1..4] OF REAL;
  991. The number in the 3rd row and 2nd column is called Z[3,2]. The 
  992. entire first row of Z is called Z[1]; the second row is called 
  993. Z[2]; etc. For example, if you say ___ 
  994. Z[5]:=Z[3];
  995. the computer will look at the real numbers in the 3rd row of Z, 
  996. and copy them into the 5th row. Suppose W is another array that 
  997. has 6 rows and 4 columns; if you say ___ 
  998. W:=Z;
  999. the computer will look at each real number in Z and copy it into 
  1000. W.
  1001.                                          You can have many kinds 
  1002. of arrays: you can have an array of REAL, an array of INTEGER, 
  1003. and even an array of CHAR.
  1004.  
  1005.                                                       STRING
  1006.                                          A variable can stand for 
  1007. a string:
  1008. VAR
  1009.    X: STRING;
  1010. BEGIN;
  1011. X:='I LOVE MY MOTH';
  1012. WRITELN(X,'ER');
  1013. END.
  1014.                                          The computer will write 
  1015. `I LOVE MY MOTH' and then `ER', like this:
  1016. I LOVE MY MOTHER
  1017.                                          You can make the 
  1018. computer read a string:
  1019. VAR
  1020.    X: STRING;
  1021. BEGIN;
  1022. WRITELN('WHAT IS YOUR NAME?');
  1023. READLN(X);
  1024. WRITELN('HELLO ',X,' THE BEAUTIFUL');
  1025. END.
  1026. When you run that program, the computer asks:
  1027. WHAT IS YOUR NAME?
  1028. Then the READLN(X) statement makes the computer wait for you to 
  1029. input a string. If you input the name MARILYN MONROE, the 
  1030. computer will write:
  1031. HELLO MARILYN MONROE THE BEAUTIFUL
  1032.                                           Other versions Instead 
  1033. of letting you say STRING, some versions of PASCAL require you to 
  1034. say STRING[80].
  1035.                                          The oldest versions of 
  1036. PASCAL require you to say PACKED ARRAY [1..80] OF CHAR instead. 
  1037. Some of those old versions can't read or write the whole array 
  1038. string at once: instead you must create FOR loops that read and 
  1039. write one character at a time.